package com.xunyin.modules.mtservicefac.rest;


import com.xunyin.api.XunyinException;
import com.xunyin.constant.Constants;
import com.xunyin.modules.mtservicefac.domain.MtServiceFac;
import com.xunyin.modules.mtservicefac.service.MtServiceFacService;
import com.xunyin.pagination.PaginationRequest;
import com.xunyin.pagination.PaginationResponse;
import com.xunyin.utils.StringUtil;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务设施管理类controller
 * <p>
 * Created by xunyinyunshang <3594419785@qq.com>
 * CopyRight 讯音云商 https://www.tonetn.com
 */
@Slf4j
@AllArgsConstructor
@Api(tags = "共享空间:服务设施类")
@RestController
@RequestMapping("api/mtservicefac")
public class BackendMtServiceFacController {

    /**
     * 服务设施服务接口
     */
    private final MtServiceFacService mtServiceFacService;

    /**
     * 获取服务设施列表
     */
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseEntity<Map<String, Object>> getList(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        List<MtServiceFac> list = mtServiceFacService.list();
        result.put("mtServiceFacList", list);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    /**
     * 服务设施列表查询
     *
     * @param request HttpServletRequest对象
     * @return 服务设施列表页面
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseEntity<Map<String, Object>> list(HttpServletRequest request) {
        String token = request.getHeader("Access-Token");
        Integer page = request.getParameter("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(request.getParameter("page"));
        Integer pageSize = request.getParameter("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
        String isDel = request.getParameter("isDel");

        PaginationRequest paginationRequest = new PaginationRequest();
        paginationRequest.setCurrentPage(page);
        paginationRequest.setPageSize(pageSize);

        Map<String, Object> params = new HashMap<>();

        paginationRequest.setSearchParams(params);
        paginationRequest.setSortColumn(new String[]{"id desc"});
        PaginationResponse<MtServiceFac> paginationResponse = mtServiceFacService.queryListByPagination(paginationRequest);
        Map<String, Object> result = new HashMap<>();
        result.put("paginationResponse", paginationResponse);

        return new ResponseEntity<>(result, HttpStatus.OK);
        //return getSuccessResult(result);
    }


    /**
     * 保存服务设施信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @CrossOrigin
    public ResponseEntity saveHandler(HttpServletRequest request, @RequestBody Map<String, Object> params) {
        String token = request.getHeader("Access-Token");
        String id = params.get("id") == null ? "0" : params.get("id").toString();
        String serviceFacName = params.get("serviceFacName") == null ? "0" : params.get("serviceFacName").toString();
        String serviceFacSort = params.get("serviceFacSort") == null ? "0" : params.get("serviceFacSort").toString();

        MtServiceFac mtServiceFac = new MtServiceFac();
        if (StringUtil.isNotEmpty(id)) {
            mtServiceFac = mtServiceFacService.queryMtServiceFacById(Integer.parseInt(id));
        }

        if (mtServiceFac == null && Integer.parseInt(id) > 0) {
            //return getFailureResult(201, "员工信息不存在");
            throw new XunyinException("服务设施信息不存在");
        }

        mtServiceFac.setServiceFacName(serviceFacName);
        mtServiceFac.setServiceFacSort(Integer.parseInt(serviceFacSort));
        mtServiceFacService.saveMtServiceFac(mtServiceFac);
        return new ResponseEntity(HttpStatus.OK);
        // return getSuccessResult(true);
    }

    /**
     * 查询服务设施信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseEntity<Map<String, Object>> getMtOpenCityInfo(HttpServletRequest request, @PathVariable("id") Integer id) {
        String token = request.getHeader("Access-Token");
        MtServiceFac mtServiceFac = mtServiceFacService.queryMtServiceFacById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("mtServiceFacInfo", mtServiceFac);
        return new ResponseEntity<>(result, HttpStatus.OK);
        //return getSuccessResult(result);
    }


    /**
     * 删除服务设施信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseEntity deleteMtServiceFac(HttpServletRequest request, @PathVariable("id") Integer id) {
        String token = request.getHeader("Access-Token");
        //mtServiceFacService.updateStatus(id, 0);
        mtServiceFacService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
        // return getSuccessResult(true);
    }

}
